extends Node2D


const DirList = [
	Vector2.LEFT, Vector2.RIGHT, Vector2.UP, Vector2.DOWN
]


##  生成的数量
export var generate_count : int = 20
##  最大x轴位置
export var max_x : int = 3
export var height : int = 20

##  当前位置
var current_pos = Vector2(0,0)
##  位置列表
var pos_list : Dictionary


func _ready():
	var default = []
	for i in range(height/2+1):
		default.push_back(Vector2(0, i))
	
	for p in default:
		pos_list[p] = true
	
	generate()
	
	var b_data = AddButton.by_list_add_to(pos_list.keys(), self)
	for p in default:
		if b_data.has(p):
			b_data[p].modulate = Color.red


func generate():
	current_pos = Vector2.ZERO
	move_next(Vector2.LEFT)
	while pos_list.size() < generate_count / 2:
		move_next()
	
	current_pos = Vector2.ZERO
	move_next(Vector2.RIGHT)
	while pos_list.size() < generate_count:
		move_next()
	
	var list = [
		Vector2.LEFT,
		Vector2.RIGHT,
		Vector2.UP,
		Vector2.DOWN,
		
		Vector2(1,1),
		Vector2(-1,-1),
		Vector2(1,-1),
		Vector2(-1,1),
	]
	
	# 去掉周围全是的空间
	var remove_list = []
	for p in pos_list.keys():
		if p.x == 0:
			continue
		var count = 0
		for i in list:
			if pos_list.has(p+i):
				count += 1
		if count >= 6:
			remove_list.push_back(p)
	
	for p in remove_list:
#		if randf() < 0.33:
			pos_list.erase(p)
	
	var rect = Rect2(Vector2(0,0), Vector2(0,0))
	for p in pos_list.keys():
		if rect.position.x > p.x:
			rect.position.x = p.x
		if rect.position.y > p.y:
			rect.position.y = p.y
		if rect.end.x < p.x:
			rect.end.x = p.x
		if rect.end.y < p.y:
			rect.end.y = p.y
	
	var dir_list = [
		Vector2.LEFT, Vector2.RIGHT, Vector2.UP, Vector2.DOWN,
		Vector2(1,1), Vector2(1, -1), Vector2(-1, -1), Vector2(-1, 1),
	]
	var cc = 0
	while true:
		cc += 1
		var add_list = insert_empty_pos(rect, dir_list, 2, 1.0)
		for p in add_list:
			pos_list[p] = true
		if add_list.size() == 0:
			break
	print(cc)
	var add_list_2 = insert_empty_pos(rect, dir_list, 3, 0.5)
	for p in add_list_2:
			pos_list[p] = true
	var add_list_3 = insert_empty_pos(rect, dir_list, 4, 0.2)
	for p in add_list_2:
			pos_list[p] = true
	


func insert_empty_pos(rect: Rect2, dir_list: Array, count_: int, ratio: float):
	var add_list = []
	for x in range(rect.position.x, rect.end.x+1):
		for y in range(rect.position.y, rect.end.y+1):
			var p = Vector2(x, y)
			var count = 0
			for dir in dir_list:
				if pos_list.has(dir + p):
					count += 1
			if count == count_ && not pos_list.has(p):
				if randf() < ratio:
					add_list.push_back(p)
	return add_list



##  移动到下一个位置
func move_next(dir=Vector2.INF):
	if dir == Vector2.INF:
		dir = DirList[randi() % DirList.size()]
	var temp_pos = current_pos + dir
	
	var d = {
		left = current_pos + Vector2.LEFT,
		right = current_pos + Vector2.RIGHT,
		up = current_pos + Vector2.UP,
		down = current_pos + Vector2.DOWN,
	}
	var list = [Vector2.LEFT, Vector2.RIGHT, Vector2.UP, Vector2.DOWN]
	if pos_list.has(d.left) || d.left.x < -max_x:
		list.erase(Vector2.LEFT)
	if pos_list.has(d.right) || d.right.x > max_x:
		list.erase(Vector2.RIGHT)
	if pos_list.has(d.up) || d.up.y < 0:
		list.erase(Vector2.UP)
	if pos_list.has(d.down):
		list.erase(Vector2.DOWN)
	
	if list.size() == 0:
		temp_pos = current_pos + Vector2.DOWN
	else:
		temp_pos = current_pos + list[randi() % list.size()]
	
	current_pos = temp_pos
	pos_list[temp_pos] = true


func _input(event):
	if event is InputEventMouseButton:
		if event.doubleclick:
			if get_tree().reload_current_scene() != OK:
				printerr("重新加载时出现错误")

